﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Text;
using AdoDAL.Entities;

//using Stiline.DAL.Entities;

namespace AdoDAL.Repositories
{
    public class UserRepository : BaseRepository
    {
        public void Save(User item)
        {
            if (item.Id == 0)
            {
                using (var connection = CreateConnection())
                {
                    connection.Open();
                    SqlCommand command = CreateStoredProcedureExecutionCommand("UserAdd", connection,
                                                                               new SqlParameter("@name", item.Name),
                                                                               new SqlParameter("@password",item.Password),
                                                                               new SqlParameter("@email", item.Email));
                    command.CommandType = CommandType.StoredProcedure;
                    command.ExecuteNonQuery();
                }
            }
            else
            {
                using (var connection = CreateConnection())
                {
                    connection.Open();
                    SqlCommand command = CreateStoredProcedureExecutionCommand("UserEdit", connection,
                                                                                new SqlParameter("@id", item.Id),
                                                                                new SqlParameter("@name", item.Name),
                                                                                new SqlParameter("@password", item.Password),
                                                                                new SqlParameter("@email", item.Email));
                    command.ExecuteNonQuery();
                }
            }
        }
        
        public User Get(int id)
        {
            return GetAll().FirstOrDefault(u => u.Id == id);
        }

        public IEnumerable<User> GetAll()
        {
            using (var connection = CreateConnection())
            {
                connection.Open();
                SqlCommand command = CreateStoredProcedureExecutionCommand("UserLoad", connection);
                SqlDataReader reader = command.ExecuteReader();

                var users = new List<User>();
                while (reader.Read())
                {
                    var user = new User();
                    user.Id = (int)reader["Id"];
                    user.Name = reader["Name"].ToString();
                    user.Password = reader["Password"].ToString();
                    user.Email = reader["Email"].ToString();

                    users.Add(user);
                }

                return users;
            }
        } 

        public void Delete(int id)
        {
            using (var connection = CreateConnection())
            {
                connection.Open();
                SqlCommand command = CreateStoredProcedureExecutionCommand("UserDelete", connection, new SqlParameter("@id", id));

                command.ExecuteNonQuery();
            }
        }

        public void Delete(Task user)
        {
            Delete(user.Id);
        }
    }
}
